/**
 * 
 */
package org.xlab.baseframework.test.service;

import static org.junit.Assert.*;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.xlab.baseframework.domain.SystemParameter;
import org.xlab.baseframework.domain.enumerate.EnumWebServiceClientType;
import org.xlab.baseframework.exception.PermissionDeniedException;
import org.xlab.baseframework.exception.RootUserNotFoundException;
import org.xlab.baseframework.exception.SystemParameterNotFoundException;
import org.xlab.baseframework.exception.SystemRuntimeException;
import org.xlab.baseframework.exception.SystemWebServiceSessionNotFoundException;
import org.xlab.baseframework.service.api.ISystemParameterService;
import org.xlab.baseframework.service.api.ISystemWebServiceSessionService;
import org.xlab.baseframework.service.spi.IUserService;
import org.xlab.baseframework.system.PlatformDeclare;
import org.xlab.baseframework.test.base.BaseTester;

/**
 * @author makubex
 * 
 */
@RunWith(BaseTester.class)
@ContextConfiguration("classpath:org/xlab/baseframework/conf/baseframework-WebModule-All.xml")
public class SystemWebServiceSessionTestCase {

	private static final Logger log = Logger
			.getLogger(SystemWebServiceSessionTestCase.class);

	@Autowired
	@Qualifier("bpf_systemWSSessionService")
	private ISystemWebServiceSessionService systemWebServiceSessionService;

	@Autowired
	@Qualifier("bpf_systemParameterService")
	private ISystemParameterService systemParameterService;

	@Autowired
	@Qualifier("bpf_defaultUserService")
	private IUserService userService;

	@Test
	public void testSimpleLogin() {
		log.debug("Testing simple login...");
		try {
			String sessionId1 = systemWebServiceSessionService.createSession(
					userService.getSystemRootUser(), "1000",
					EnumWebServiceClientType.BROWSER);
			String sessionId2 = systemWebServiceSessionService.createSession(
					userService.getSystemRootUser(), "1000",
					EnumWebServiceClientType.BROWSER);
			SystemParameter sso = this.systemParameterService.findByCode(
					PlatformDeclare.PARAM_WEBSERVICE_SINGLESESSION_CODE, true);
			assertEquals("FALSE", sso.getValue());
			assertNotSame(sessionId1, sessionId2);
			this.systemWebServiceSessionService.deleteSession(sessionId2,
					userService.getSystemRootUser());
			sso.setValue("TRUE");
			this.systemParameterService.updateNonSystemParameter(sso,
					userService.getSystemRootUser());
			sessionId2 = systemWebServiceSessionService.createSession(
					userService.getSystemRootUser(), "1000",
					EnumWebServiceClientType.BROWSER);
			assertEquals(sessionId1, sessionId2);
		} catch (RootUserNotFoundException e) {
			throw new SystemRuntimeException(e);
		} catch (SystemParameterNotFoundException e) {
			e.printStackTrace();
		} catch (SystemRuntimeException e) {
			e.printStackTrace();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (PermissionDeniedException e) {
			e.printStackTrace();
		} catch (SystemWebServiceSessionNotFoundException e) {
			e.printStackTrace();
		}
	}
}
